package com.qifan.alb2b.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Session验证filter
 */
public class SessionFilter implements Filter {
	public static List<String> urls=new ArrayList<String>();
	
	/** 初始化需要前台session过滤的url */
	static{
		urls.add("/route/queryRoute.usr");
		urls.add("/book/toBook.usr");
		urls.add("/book/getPriceTable.usr");
		urls.add("/book/book.usr");
		urls.add("/book/printOrder.usr");
		urls.add("/userCenter/query.usr");
		urls.add("/userCenter/update.usr");
		urls.add("/userCenter/queryUser.usr");
		urls.add("/userCenter/queryOrder.usr");
		urls.add("/userCenter/updatePassword.usr");
		urls.add("/pages/front/book/");
		urls.add("/pages/front/user/");
		urls.add("/order/query.usr");
		urls.add("/order/updateStatus.usr");
		urls.add("/order/queryOrderDetail.usr");
	}
	
	public void destroy() {

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest httpRequest=(HttpServletRequest) request;
		HttpServletResponse httpResponse=(HttpServletResponse) response;
		String url = httpRequest.getRequestURI();
		
		//前台session验证拦截判断
		if(isContains(url)){
			if(httpRequest.getSession() != null && httpRequest.getSession().getAttribute(Constants.SESSION_USER_VO) != null){//session不为空 
				chain.doFilter(httpRequest, response);
				return;
			}else{//session为空
				if(url.contains("/user/login.usr")||url.contains("/user/logout.usr")
						||url.contains("/front/index.jsp")||url.contains("/getVcode.usr")||url.contains("/register.jsp")){
					chain.doFilter(httpRequest, response);
					return;
				}else
					httpResponse.sendRedirect(httpRequest.getContextPath()+"/pages/front/index.jsp?login=true");
			}
		}else if(url.endsWith(".adm")||url.matches(".*/back/.*\\.jsp")||url.matches(".*/back/")){//后台Session验证拦截
			if(httpRequest.getSession() != null && httpRequest.getSession().getAttribute(Constants.SESSION_ADMIN) != null){//session不为空 
				chain.doFilter(httpRequest, response);
				return;
			}else{//session为空
				if(url.contains("/admin/login.adm")||url.contains("/admin/logout.adm")||url.contains("/back/login.jsp")){
					chain.doFilter(httpRequest, response);
					return;
				}else
					httpResponse.sendRedirect(httpRequest.getContextPath()+"/pages/back/login.jsp");
			}
		}
		chain.doFilter(httpRequest, httpResponse);

	}

	public void init(FilterConfig filterConfig) throws ServletException {
		
	}
	
	private boolean isContains(String url){
		for(int i=0;i<urls.size();i++){
			if(url.matches(".*"+urls.get(i)+".*"))
				return true;
		}
		return false;
	}

}
